/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uit.ieproject.dataaccess;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import uit.ieproject.entities.Activity;
import uit.ieproject.utilites.DbConnection;
import uit.ieproject.utilites.Info;

/**
 *
 * @author Maivia
 */
public class ActivityDAO {

    public void addActivity(Activity activity) throws SQLException {
        Connection con = DbConnection.getConnection();               
        StringBuffer sql = new StringBuffer();
        sql.append("INSERT INTO ");
        sql.append(Info.TABLE_ACTIVITY);
        sql.append(" (personId, time, act) VALUES (?,?,?)");
        PreparedStatement stmt = con.prepareStatement(sql.toString());
        stmt.setLong(1, activity.getPersonId());
        stmt.setString(2, activity.getTime());
        stmt.setString(3, activity.getAct());
        stmt.executeUpdate();
        stmt.close();
        con.close();
    }

    public void deleteActivity(Long personId) throws SQLException {
        Connection con = DbConnection.getConnection();       
        StringBuffer sql = new StringBuffer();
        sql.append("DELETE FROM ");
        sql.append(Info.TABLE_ACTIVITY);
        sql.append(" WHERE personId = ?");
        PreparedStatement stmt = con.prepareStatement(sql.toString());
        stmt.setLong(1, personId);
        stmt.executeUpdate();
        stmt.close();
        con.close();
    }
    
    public ArrayList<Activity> getAllActivitiesOfPerson(Long id) throws SQLException {
        //Map<String, String> activities = new HashMap<>();
        ArrayList<Activity> activities = new ArrayList<>();
        Connection con = DbConnection.getConnection();       
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT * FROM ");
        sql.append(Info.TABLE_ACTIVITY);
        sql.append(" WHERE personId = ?");
        PreparedStatement stmt = con.prepareStatement(sql.toString());
        stmt.setLong(1, id);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            Activity activity = new Activity();
            activity.setId(rs.getLong(1));
            activity.setPersonId(rs.getLong(2));
            activity.setTime(rs.getNString(3));
            activity.setAct(rs.getNString(4));
            activities.add(activity);
        }
        rs.close();
        stmt.close();
        con.close();
        return activities;
    }
    
    public String getAnswerAboutActivity(String sql, String timeAsk) throws  SQLException{
        StringBuilder answer = new StringBuilder();
        Connection con = DbConnection.getConnection();
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while(rs.next()){
            
            if(timeAsk.equals("all")){
                //Get All activities of person
                answer.append(rs.getNString(3)).append(" : ");
                answer.append(rs.getNString(4));
                answer.append("\n");
            }else{
                //only get activity at time= "time"
                String time = rs.getNString(3);
                Pattern pattern = Pattern.compile("[0-9]{3,4}");
                Matcher matcher = pattern.matcher(time);
                while(matcher.find()){
                    String matches = matcher.group().trim();              
                    if(matches.equals(timeAsk)){
                        String activity = rs.getNString(4);
                        answer.append(activity);
                    }
                }
            }
        }
        rs.close();
        stmt.close();
        con.close();
        return answer.toString();
    }
}
